package gui;

import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class QRDisplay extends JFrame implements MouseListener
{
	private static final long serialVersionUID = -7131697544886767814L;
	BufferedImage myImage = null;
	int multi = 1;
	int mWidth = 1;
	int mHeight = 1;
	
	private BufferedImage createImage(String name)
	{
		try 
		{
			return ImageIO.read(getClass().getResourceAsStream("/resources/" + name));
		} 
		catch (IOException e) 
		{
			return null;
		}
	}
	
	public QRDisplay(BufferedImage image)
	{
		super("InstaDir QR");
		myImage = image;
		
		GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
		int width = gd.getDisplayMode().getWidth();
		int height = gd.getDisplayMode().getHeight();
		
		multi = (int)(height * 0.9f) / myImage.getHeight();
		if(multi <= 0)
			multi = 1;
		
		mWidth = myImage.getWidth() * multi;
		mHeight = myImage.getHeight() * multi;
		
		setSize(mWidth, mHeight);
		setResizable(false);
		
		setIconImage(createImage("icons/icon-dark.png"));
		setLocation((width - mWidth) / 2, (height - mHeight) / 2);
		setVisible(true);
		
		addMouseListener(this);
	}
	
	public void paint(Graphics g)
	{
		g.drawImage(myImage, 0, 0, mWidth, mHeight, null);
	}

	@Override
	public void mouseClicked(MouseEvent arg0)
	{
		setVisible(false);
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {}
	@Override
	public void mouseExited(MouseEvent arg0) {}
	@Override
	public void mousePressed(MouseEvent arg0) {}
	@Override
	public void mouseReleased(MouseEvent arg0) {}
}
